package net.monare.mifss.evn;

import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.win.ConfigureStorageWindow;
import net.monare.mifss.admin.ui.win.RemoveApiKeyWindow;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.exceptions.StorageServiceException;

/**
 * Created by jernej on 4/22/14.
 */
public class RemoveSelectedApiKeyListener extends BasicMifssListener implements Button.ClickListener {

    private final ConfigureStorageWindow parent;
    private RemoveApiKeyWindow parentWindow;
    private ApiKey apiKey;
    private String storageDefName;

    public RemoveSelectedApiKeyListener(ConfigureStorageWindow parent, RemoveApiKeyWindow parentWindow, ApiKey apiKey, String storageDefName) {

        this.parentWindow = parentWindow;
        this.parent = parent;
        this.apiKey = apiKey;
        this.storageDefName = storageDefName;
    }

    @Override
    public void buttonClick(Button.ClickEvent event) {

        try {

            getMifssHolder().getStorageService().removeApiKey(storageDefName, apiKey);

            parent.resetAllPanes();
            Notification.show(MifssMessages.getMessage("sys.success.api.key.removed"));
            parentWindow.close();

        } catch (StorageServiceException e) {
            Notification.show(MifssMessages.getMessage("sys.error.api.key.remove") + " : " + e.getMessage());
        }
    }
}
